package com.psl.services.work;

import com.psl.Dao.Conn;
import com.psl.model.halfproduct;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class getHname {
    public List get(String h_name, String h_size) {
        Connection conn = new Conn().getConn();
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        List product = new ArrayList();
        try {
            pstmt = conn.prepareStatement("SELECT h_no,h_name,h_size from halfproduct_list WHERE h_name LIKE ? and h_size=?");
            pstmt.setString(1, "%" + h_name + "%");
            pstmt.setString(2, h_size);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                halfproduct p = new halfproduct();
                p.setH_no(rs.getString(1));
                p.setH_name(rs.getString(2));
                p.setH_size(rs.getString(3));
                product.add(p);
                HttpServletRequest request = ServletActionContext.getRequest();
                request.setAttribute("query", product);
            }
            return product;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (rs != null) {
                    rs.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
